package com.imooc.distributedemo.controller;

import com.imooc.distributedemo.common.RedisLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@Slf4j
@RestController
public class RedisLockController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/redisLock")
    public String redisLockConn(){
        log.info("我进入了方法");
        String key = "redisKey";
        String value = UUID.randomUUID().toString();


            log.info("我进入了锁");
            try(RedisLock redisLock = new RedisLock(redisTemplate,key,30)){
                if(redisLock.getLock()){
                    Thread.sleep(10000);
                }
            }catch (InterruptedException ex){
                log.error("线程获取错误",ex);
            }catch (Exception e){
                log.error("出现错误",e);
            }

        log.info("方法执行完成");
        return "方法执行完成";
    }
}
